#coding:utf-8
'''
create on 2017-04-20

@author:sandy
'''

import os
from sys import argv
import json
server = json.load(file("shell.json"),encoding='utf-8')
def start():
    print "################### start ###################"
    for dir in server:
        os.chdir("./../" + dir)
        pyc = "./" + "main.pyc"
        exit = os.path.exists(pyc)
        if exit:
            os.system("python main.pyc start")
        else:
            os.system("python main.py start")
        os.chdir("./../shell/")
    print "###################  end  ###################"

def stop():
    revert = reversed(server)
    print "################### stop ###################"
    for dir in revert:
        os.chdir("./../" + dir)
        pyc = "./" + "main.pyc"
        exit = os.path.exists(pyc)
        if exit:
            os.system("python main.pyc stop")
        else:
            os.system("python main.py stop")
        os.chdir("./../shell/")
    print "###################  end  ###################"
    
def restart():
    stop()
    start()


if __name__ == '__main__':
    script,cmd = argv
    if cmd == "start":
        start()
    if cmd == "stop":
        stop()
    if cmd == "restart":
        restart()



    